package com.ty.xiaoyu.controller;

import com.ty.xiaoyu.entity.Notify;
import com.ty.xiaoyu.entity.NotifyModule;
import com.ty.xiaoyu.exception.NotifyOperationException;
import com.ty.xiaoyu.util.MyTimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/notify")
@Slf4j
public class NotifyController extends BaseController {

    /**
     * 获取未读通知总和
     * @return
     */
    @GetMapping("/getAllNotifyNum")
    private Map<String, Object> getNotifyNum() {
        log.info(MyTimeUtil.nowTime() + "=== getNotifyNum star ===");
        Map<String, Object> modelMap = new HashMap<>();
        Map<String, Object> data = new HashMap<>();
        Integer notifyListNum = notifyService.getAllNotifyListCount(loginUser.getUserId());
        data.put("notifyListNum", notifyListNum);
        modelMap.put("code", 0);
        modelMap.put("msg", "成功");
        modelMap.put("data", data);
        log.info(MyTimeUtil.nowTime() + "=== getNotifyNum end ===");
        return modelMap;
    }

    /**
     * 删除通知
     * @param notifyId
     * @return
     */
    @DeleteMapping("/cancelNotify")
    private Map<String, Object> cancelNotify(@RequestParam("notifyId") String notifyId) {
        log.info(MyTimeUtil.nowTime() + "=== cancelNotify star ===");
        Map<String, Object> modelMap = new HashMap<>();
        notifyService.cancelNotify(notifyId);
        modelMap.put("code", 0);
        modelMap.put("msg", "成功");
        modelMap.put("data", null);
        log.info(MyTimeUtil.nowTime() + "=== cancelNotify end ===");
        return modelMap;
    }

    /**
     * 获取所有通知
     * @return
     */
    @GetMapping("/getAllNotify")
    private Map<String, Object> getNotify() {
        log.info(MyTimeUtil.nowTime() + "=== getNotify star ===");
        Map<String, Object> modelMap = new HashMap<>();
        Map<String, Object> data = new HashMap<>();
        List<Notify> notifyList = null;
        try {
            notifyList = notifyService.getAllNotifyList(loginUser.getUserId());
        } catch (NotifyOperationException e) {
            modelMap.put("code", 102);
            modelMap.put("msg", e.getMessage());
            modelMap.put("data", null);
            log.error(MyTimeUtil.nowTime() + "=== getNotify error ===" + e.getMessage());
            return modelMap;
        }
        // 放入通知目标帖子的内容
        for (Notify notify : notifyList) {
            if (notify.getTopicId() != null) {
                notify.setTopic(topicService.getTopicById(notify.getTopicId()));
                // 放入帖子图片
                notify.getTopic().setTopicImageList(topicService.getTopicImageList(notify.getTopicId()));
            } else if (notify.getReplyId() != null) {
                notify.setReply(replyService.getReplyById(notify.getReplyId()));
            }
        }
        data.put("notifyList", notifyList);
        modelMap.put("code", 0);
        modelMap.put("msg", "成功");
        modelMap.put("data", data);
        log.info(MyTimeUtil.nowTime() + "=== getNotify end ===");
        return modelMap;
    }

    /**
     * 获取某类别的通知列表
     * @param notifyModuleId
     * @return
     */
    @GetMapping("getNotify")
    private Map<String, Object> getNotify(@RequestParam("notifyModuleId") String notifyModuleId) {

        log.info(MyTimeUtil.nowTime() + "=== getNotify star ===");
        Map<String, Object> modelMap = new HashMap<>();
        Map<String, Object> data = new HashMap<>();
        List<Notify> notifyList;
        try {
            notifyList = notifyService.getNotifyList(loginUser.getUserId(), notifyModuleId);
        } catch (NotifyOperationException e) {
            modelMap.put("code", 102);
            modelMap.put("msg", e.getMessage());
            modelMap.put("data", null);
            log.error(MyTimeUtil.nowTime() + "=== getNotify error ===" + e.getMessage());
            return modelMap;
        }
        // 放入通知目标帖子的内容
        for (Notify notify : notifyList) {
            if (notify.getTopicId() != null) {
                notify.setTopic(topicService.getTopicById(notify.getTopicId()));
                // 放入帖子图片
                notify.getTopic().setTopicImageList(topicService.getTopicImageList(notify.getTopicId()));
            } else if (notify.getReplyId() != null) {
                notify.setReply(replyService.getReplyById(notify.getReplyId()));
            }
        }
        // 更新通知已阅读状态
        notifyService.modifyNotifyIsReadByUserIdAndModuleId(loginUser.getUserId(), notifyModuleId);
        data.put("notifyList", notifyList);
        modelMap.put("code", 0);
        modelMap.put("msg", "成功");
        modelMap.put("data", data);
        log.info(MyTimeUtil.nowTime() + "=== getNotify end ===");
        return modelMap;
    }

    /**
     * 获取各通知类别及未读通知数量
     * @return
     */
    @GetMapping("getNotifyModuleList")
    private Map<String, Object> getNotifyModuleList() {
        log.info(MyTimeUtil.nowTime() + "=== getNotifyModuleList star ===");
        Map<String, Object> modelMap = new HashMap<>();
        Map<String, Object> data = new HashMap<>();
        List<NotifyModule> notifyModuleList = notifyModuleService.getNotifyModuleList(loginUser.getUserId());
        data.put("notifyModuleList", notifyModuleList);
        modelMap.put("code", 0);
        modelMap.put("msg", "成功");
        modelMap.put("data", data);
        log.info(MyTimeUtil.nowTime() + "=== getNotifyModuleList end ===");
        return modelMap;
    }


    // =====================================================

    /**
     * 获取各通知分类未读的数量
     * @return
     */
    @GetMapping("/getNotifyListNum")
    private Map<String, Object> getNotifyListNum() {
        log.info(MyTimeUtil.nowTime() + "=== getNotifyListNum star ===");
        Map<String, Object> modelMap = new HashMap<>();
        Map<String, Object> data = new HashMap<>();
        Integer announceNum = notifyService.getAnnounceListCount(loginUser.getUserId());
        Integer messageNum = notifyService.getMessageListCount(loginUser.getUserId());
        Integer greatNum = notifyService.getGreatListCount(loginUser.getUserId());
        Integer commentNum = notifyService.getCommentListCount(loginUser.getUserId());
        data.put("announceNum", announceNum);
        data.put("messageNum", messageNum);
        data.put("greatNum", greatNum);
        data.put("commentNum", commentNum);
        modelMap.put("code", 0);
        modelMap.put("msg", "成功");
        modelMap.put("data", data);
        log.info(MyTimeUtil.nowTime() + "=== getNotifyListNum end ===");
        return modelMap;
    }

    /**
     * 获取公告列表
     * @return
     */
    @GetMapping("/getAnnounceList")
    private Map<String, Object> getAnnounceList() {
        log.info(MyTimeUtil.nowTime() + "=== getAnnounceList star ===");
        Map<String, Object> modelMap = new HashMap<>();
        Map<String, Object> data = new HashMap<>();
        List<Notify> announceList = notifyService.getAnnounceList(loginUser.getUserId());
        data.put("announceList", announceList);
        modelMap.put("code", 0);
        modelMap.put("msg", "成功");
        modelMap.put("data", data);
        log.info(MyTimeUtil.nowTime() + "=== getAnnounceList end ===");
        return modelMap;
    }

    /**
     * 获取消息列表
     * @return
     */
    @GetMapping("/getMessageList")
    private Map<String, Object> getMessageList() {
        log.info(MyTimeUtil.nowTime() + "=== getMessageList star ===");
        Map<String, Object> modelMap = new HashMap<>();
        Map<String, Object> data = new HashMap<>();
        List<Notify> messageList = notifyService.getMessageList(loginUser.getUserId());
        data.put("messageList", messageList);
        modelMap.put("code", 0);
        modelMap.put("msg", "成功");
        modelMap.put("data", data);
        log.info(MyTimeUtil.nowTime() + "=== getMessageList end ===");
        return modelMap;
    }

    /**
     * 获取点赞列表
     * @return
     */
    @GetMapping("/getGreatList")
    private Map<String, Object> getGreatList() {
        log.info(MyTimeUtil.nowTime() + "=== getGreatList star ===");
        Map<String, Object> modelMap = new HashMap<>();
        Map<String, Object> data = new HashMap<>();
        List<Notify> greatList = notifyService.getGreatList(loginUser.getUserId());
        data.put("greatList", greatList);
        modelMap.put("code", 0);
        modelMap.put("msg", "成功");
        modelMap.put("data", data);
        log.info(MyTimeUtil.nowTime() + "=== getGreatList end ===");
        return modelMap;
    }

    /**
     * 获取评论列表
     * @return
     */
    @GetMapping("/getCommentList")
    private Map<String, Object> getCommentList() {
        log.info(MyTimeUtil.nowTime() + "=== getCommentList star ===");
        Map<String, Object> modelMap = new HashMap<>();
        Map<String, Object> data = new HashMap<>();
        List<Notify> commentList = notifyService.getCommentList(loginUser.getUserId());
        data.put("commentList", commentList);
        modelMap.put("code", 0);
        modelMap.put("msg", "成功");
        modelMap.put("data", data);
        log.info(MyTimeUtil.nowTime() + "=== getCommentList end ===");
        return modelMap;
    }
}
